package com.doit.doit26;

import com.doit.beans.Emp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PreparedStatementDemo {

  public static void main(String[] args) throws Exception {
    //注册驱动
    Class.forName("com.mysql.jdbc.Driver");
    //获取连接
    Connection conn = DriverManager
        .getConnection("jdbc:mysql://127.0.0.1:3306/lvxiaolong", "root", "123456");
    //创建执行sql的对象
    PreparedStatement ps = conn.prepareStatement("select * from t_emp where eid=?");
    ps.setInt(1, 2);
    ResultSet resultSet = ps.executeQuery();
    //循环取值
    List<Emp> empList = new ArrayList<Emp>();
    while (resultSet.next()) {
      int eid = resultSet.getInt(1);
      String name = resultSet.getString(2);
      String sex = resultSet.getString(3);
      int idNo = resultSet.getInt(4);
      int did = resultSet.getInt(5);
      Emp emp = new Emp(eid, name, sex, idNo, did);
      empList.add(emp);
    }
    resultSet.close();
    ps.close();
    conn.close();

    for (Emp e : empList) {
      System.out.println(e);
    }
  }


}
